﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using DTO;

namespace DAO
{
    public class DonGiaVe_DAO
    {
        public static DonGiaVe_DTO CreateDonGiaDTO(DonGiaVe dongia)
        {
            DonGiaVe_DTO dongiaDTO = new DonGiaVe_DTO();
            Mapper.CreateMap<DonGiaVe, DonGiaVe_DTO>();
            dongiaDTO = Mapper.Map<DonGiaVe, DonGiaVe_DTO>(dongia);
            return dongiaDTO;
        }


        public List<DonGiaVe_DTO> GetDonGiaVeDetails(string mMaTuyenBay)
        {
            List<DonGiaVe_DTO> listDTO = new List<DonGiaVe_DTO>();
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                var query = from tr in context.DonGiaVes
                            where tr.MaTuyenBay == mMaTuyenBay
                            select tr;
                foreach (var cb in query)
                {
                    DonGiaVe_DTO DTO = CreateDonGiaDTO(cb);
                    listDTO.Add(DTO);
                }
            }
            catch (System.Exception)
            {
            }
            return listDTO;
        }

        public DonGiaVe_DTO GetDonGiaDetails(string maTuyenBay, string maHangVe)
        {
            DonGiaVe_DTO dongiaDTO = new DonGiaVe_DTO();
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                var query = from dg in context.DonGiaVes
                            where dg.MaTuyenBay == maTuyenBay && dg.MaHangVe == maHangVe
                            select dg;
                DonGiaVe dongiaEF = query.First();
                dongiaDTO = CreateDonGiaDTO(dongiaEF);
            }
            catch (System.Exception)
            { }
            return dongiaDTO;
        }

        public static string DonGia(string maTuyenBay, string maHangVe)
        {
            string dongia = "";
            QLCBDataContext context = new QLCBDataContext();
            var query = from dg in context.DonGiaVes
                           where dg.MaTuyenBay == maTuyenBay && dg.MaHangVe == maHangVe
                           select new { dg.DonGia };
            foreach (var x in query)
            {
                dongia = x.DonGia.ToString();
            }
            return dongia;
        }

        public void InsertDonGiaVe(DonGiaVe_DTO DTO)
        {
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                DonGiaVe dongia = new DonGiaVe();
                dongia.MaTuyenBay = DTO.MaTuyenBay;
                dongia.MaHangVe = DTO.MaHangVe;
                dongia.DonGia = DTO.DonGia;
                context.DonGiaVes.InsertOnSubmit(dongia);
                context.SubmitChanges();
            }
            catch (System.Exception ex)
            {
                string str = ex.Message;
            }
        }

        public void UpdateDonGiaVe(DonGiaVe_DTO DTO)
        {
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                DonGiaVe dongia = new DonGiaVe();
                dongia = context.DonGiaVes.Single(tt => tt.MaDonGia == DTO.MaDonGia);
                dongia.MaTuyenBay = DTO.MaTuyenBay;
                dongia.MaHangVe = DTO.MaHangVe;
                dongia.DonGia = DTO.DonGia;
                context.SubmitChanges();
            }
            catch (System.Exception ex)
            {
                string str = ex.Message;
            }
        }

        public void DeleteDonGiaVe(DonGiaVe_DTO DTO)
        {
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                DonGiaVe dongia = context.DonGiaVes.Single(tr => tr.MaDonGia == DTO.MaDonGia);
                context.DonGiaVes.DeleteOnSubmit(dongia);
                context.SubmitChanges();
            }
            catch (System.Exception ex)
            {
                string str = ex.Message;
            }
        }
    }
}
